package com.ailiwu.www.ailiwu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.adapter.PostsAdapter;
import com.ailiwu.www.ailiwu.base.BaseActivity;
import com.ailiwu.www.ailiwu.bean.PostsBean;
import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.melnykov.fab.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;

/**
 * Author:    XiYoung
 * Version    V1.0
 * Date:      2015/12/6 000614:38
 * Description:
 * Modification  History:
 * Date         	Author        		Version        	Description
 * -----------------------------------------------------------------------------------
 * 2015/12/6 0006       XiYoung              1.0                   1.0
 * Why & What is modified:
 */
public class PostRecylerActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    private List<PostsBean.DataEntity.PostsEntity> list = new ArrayList<>();
    private PostsAdapter myRecyclerViewAdapter;
    private RecyclerView mrRecyclerView;
    private Toolbar toolbar;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private LinearLayoutManager linearLayoutManager;
    private boolean isReflash;
    private String next_url;
    private TextView toolbar_title;


    @Override
    protected void loadXml() {
        setContentView(R.layout.activity_recycler_);
    }

    @Override
    protected void getIntentData(Bundle savedInstanceState) {
//
    }

    @Override
    protected void initView() {
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.mainColor, R.color.mainColor, R.color.mainColor);
        mSwipeRefreshLayout.setOnRefreshListener(this);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar_title = (TextView)  findViewById(R.id.toolbar_title);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        mrRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(this);
        mrRecyclerView.setLayoutManager(linearLayoutManager);
        myRecyclerViewAdapter = new PostsAdapter(this, list);
        mrRecyclerView.setAdapter(myRecyclerViewAdapter);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.attachToRecyclerView(mrRecyclerView);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                linearLayoutManager.smoothScrollToPosition(mrRecyclerView, null, 0);
            }
        });

    }

    @Override
    protected void setListener() {
        mrRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
                int itemCount = linearLayoutManager.getItemCount();
                if (lastVisibleItemPosition + 1 == itemCount) {
                    //加载下一页内容
                    if (!isReflash) {
                        updataRecyclerData(next_url);
                    }
                }
            }
        });
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        String url = intent.getStringExtra("urlID");
        String title = intent.getStringExtra("title");
        toolbar_title.setText(title);
        updataRecyclerData(url);
    }

    private void updataRecyclerData(String url) {
        if (url != null) {
            isReflash = true;
            httpUtils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    isReflash = false;
                    String json = responseInfo.result;
                    if (json != null) {
                        PostsBean postsBean = new Gson().fromJson(json, PostsBean.class);
                        list.addAll(postsBean.getData().getPosts());
                        myRecyclerViewAdapter.notifyDataSetChanged();
                        next_url = postsBean.getData().getPaging().getNext_url();
                    }
                }

                @Override
                public void onFailure(HttpException e, String s) {
                    Snackbar.make(mrRecyclerView, "网络连接失败", Snackbar.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            public void run() {
                if (mSwipeRefreshLayout.isRefreshing()) {
                    mSwipeRefreshLayout.setRefreshing(false);
                }
            }
        }, 2000);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
